﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using Sunny.UI;

namespace RclSystem
{
    public partial class SalaryDetail : UIForm
    {
        private string companyname = "";
        private string salesman = "";
        private double tatol = 0;
        private string sdate = "";
        private string edate = "";
        List<SalaryVo> sortedVolist = new List<SalaryVo>();
        public SalaryDetail(string companyname, string salesman, double total, string sdate, string edate)
        {
            InitializeComponent();
            this.companyname = companyname;
            this.salesman = salesman;
            this.sdate = sdate;
            this.edate = edate;
            this.Text = "工资单明细(合计:" + total + ")";
        }

        private void queryAccounts()
        {
            sortedVolist = new List<SalaryVo>();
            uiDataGridView1.Rows.Clear();
            List<SalaryVo> volist = new List<SalaryVo>();

            //获取发货单列表
            string extsql = "";
            if (salesman != "")
            {
                int offset = 0;
                string[] saleman = salesman.Split(",");
                extsql += " (";
                foreach (string sname in saleman)
                {
                    if(offset == 0)
                    {
                        extsql += " salesman = '" + salesman + "'";
                    }
                    else
                    {
                        extsql += " or salesman = '" + salesman + "'";
                    }
                    offset++;
                }
                extsql += ")";
            }
            if (companyname != "")
            {
                extsql += " and companyname = '" + companyname + "'";
            }
            DataTable dt_deliver = MySQLHelper.ExecuteQuery("SELECT * FROM tbl_delivery where " + extsql +" and deliverdate >= '" + sdate
                    + "' and deliverdate <= '" + edate + "' order by deliverdate");
            
            if (dt_deliver.Rows.Count > 0)
            {
                foreach (DataRow row in dt_deliver.Rows)
                {
                    SalaryVo vo = new SalaryVo();
                    vo.companyname = row["companyname"].ToString();
                    vo.salesman = row["salesman"].ToString();
                    vo.deliverdate = (DateTime)row["deliverdate"];
                    vo.optdate = (DateTime)row["deliverdate"];
                    vo.count = double.Parse(row["salecount"].ToString());
                    vo.nprice = double.Parse(row["saleamount"].ToString());
                    vo.bsalary = double.Parse(row["bsalary"].ToString());
                    vo.commission = double.Parse(row["settlement"].ToString());
                    vo.acceptance = double.Parse(row["interestsubsidy"].ToString());
                    vo.etc = double.Parse(row["tcamount"].ToString());
                    volist.Add(vo);
                }
            }

            //获取收款单列表
            DataTable dt_receipt = MySQLHelper.ExecuteQuery("SELECT * FROM tbl_receipt where " + extsql + " and  receiptdate >= '" + sdate
                        + "' and receiptdate <= '" + edate + "' order by receiptdate");
            //期初数结算金额
            double initaljs = 0;
            if (dt_receipt.Rows.Count > 0)
            {
                foreach (DataRow row in dt_receipt.Rows)
                {
                    SalaryVo vo = new SalaryVo();
                    vo.companyname = row["companyname"].ToString();
                    vo.salesman = row["salesman"].ToString();
                    vo.receiptdate = (DateTime)row["receiptdate"];
                    vo.optdate = (DateTime)row["receiptdate"];
                    vo.receiptprice = double.Parse(row["receiptamount"].ToString());
                    vo.penalty = double.Parse(row["interest"].ToString());
                    if(row["parentid"].ToString() == "0")
                    {
                        initaljs += vo.receiptprice;
                    }
                    volist.Add(vo);
                }
            }

            //判断是否有期初数
            //获取单位信息
            DataTable dt_company = MySQLHelper.ExecuteQuery("SELECT * FROM tbl_company where name = '" + companyname + "'");
            if (dt_company.Rows.Count > 0)
            {
                double initalamount = double.Parse(dt_company.Rows[0]["initalamount"].ToString());
                SalaryVo vo = new SalaryVo();
                vo.companyname = companyname;
                vo.salesman = "期初数";
                vo.optdate = DateTime.Parse("0001-01-01");
                vo.nprice= initalamount + initaljs;
                volist.Add(vo);
            }

            //对清单进行排序-按日期升序
            sortedVolist = volist.OrderBy(p => p.optdate).ToList();
            //显示
            foreach (SalaryVo vo in sortedVolist)
            {
                int index = this.uiDataGridView1.Rows.Add();
                this.uiDataGridView1.Rows[index].Cells[0].Value = index + 1;
                this.uiDataGridView1.Rows[index].Cells[1].Value = vo.companyname;
                this.uiDataGridView1.Rows[index].Cells[2].Value = vo.salesman;
                this.uiDataGridView1.Rows[index].Cells[3].Value = vo.optdate.ToString("yyyy-MM-dd") == "0001-01-01"?"": vo.optdate.ToString("yyyy-MM-dd");                
                this.uiDataGridView1.Rows[index].Cells[4].Value = vo.count.ToString() == "0" ? "" : vo.count.ToString();
                this.uiDataGridView1.Rows[index].Cells[5].Value = vo.nprice.ToString() == "0" ? "" : vo.nprice.ToString();
                this.uiDataGridView1.Rows[index].Cells[6].Value = vo.receiptprice.ToString() == "0" ? "" : vo.receiptprice.ToString();
                this.uiDataGridView1.Rows[index].Cells[7].Value = vo.bsalary.ToString() == "0" ? "" : vo.bsalary.ToString();
                this.uiDataGridView1.Rows[index].Cells[8].Value = vo.commission.ToString() == "0" ? "" : vo.commission.ToString();
                this.uiDataGridView1.Rows[index].Cells[9].Value = vo.penalty.ToString() == "0" ? "" : vo.penalty.ToString();
                this.uiDataGridView1.Rows[index].Cells[10].Value = vo.acceptance.ToString() == "0" ? "" : vo.acceptance.ToString();
                this.uiDataGridView1.Rows[index].Cells[11].Value = vo.etc.ToString() == "0" ? "" : vo.etc.ToString();
            }

        }

        private void SalaryDetail_Load(object sender, EventArgs e)
        {
            queryAccounts();
        }
    }
}
